package com.testaidl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

/**
 * Auth：yujunyao
 * Since: 2016/5/31 16:00
 * Email：yujunyao@yonglibao.com
 */
public class MainActivity extends AppCompatActivity {

    private Connection remoteConnection;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, ConnectionService.class);
        startService(intent);//可以启动service之后，在绑定

        bindService(intent,
                serviceConnection, Context.BIND_AUTO_CREATE);

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    remoteConnection.connect();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    remoteConnection.connecting();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    remoteConnection.connected();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            remoteConnection = Connection.Stub.asInterface(service);
            try {
                remoteConnection.bindData(display);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            remoteConnection = null;
        }
    };


    private com.testaidl.Display display = new com.testaidl.Display.Stub() {
        @Override
        public void displayToast(String content) throws RemoteException {
            displayContent(content);
        }

        @Override
        public void setData(Person person) throws RemoteException {

        }
    };

    private void displayContent(String tips) {
        Toast.makeText(this, tips, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }
}
